package io.wmy.supex.framework.support.spring.error;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求错误异常处理
 */
@ControllerAdvice
public class ExceptionController {

    /**
     * 无法找到页面错误
     * @param ex
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String noHandlerFoundException(Exception ex) {
        return "404";
    }

    /**
     * 服务器内部错误
     * @param req
     * @param ex
     * @return
     */
    @ExceptionHandler({RuntimeException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView runtimeException(HttpServletRequest req, Exception ex) {
        ModelAndView model = new ModelAndView();
        model.addObject("ex", ex);
        model.addObject("url", req.getRequestURL());
        model.setViewName("500");
        return model;
    }

}
